package com.scalar.chiptrack.adaptor.carsem;


import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;
import java.io.BufferedReader;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 17 Dec, 2009
 * Time: 3:42:49 PM
 */
public class CarsemHtmlParser extends HTMLEditorKit.ParserCallback
{
    private String m_downloadLink1 = null;
    private String m_downloadLink2 = null;
    private String m_downloadLink = null;
    private String m_fileName = null;
    private boolean firstTime = false;
    public CarsemHtmlParser()
    {

    }
    public void parse(BufferedReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD("HTML");
        DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);
    }
    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        firstTime = false;
        // For getting the Latest for ADAPTOR
        if(t == HTML.Tag.A)
        {
            HTML.Attribute value = HTML.getAttributeKey("href");

            if ( a.getAttribute(value) != null && a.getAttribute(value).toString() != null  )
            {
                if ( m_downloadLink1 == null)
                {
                    setDownloadLink1( a.getAttribute(value).toString() );
                    firstTime = true;
                }
                if ( !firstTime && m_downloadLink1 != null && m_downloadLink2 == null )
                {
                    setDownloadLink2( a.getAttribute(value).toString() );
                    
                    if (m_downloadLink1.indexOf(".") != -1 && m_downloadLink2.startsWith(m_downloadLink1.substring(0, (m_downloadLink1.indexOf(".")-1 ) ) ) )
                    {
                        setDownloadLink( m_downloadLink2 );
                    }
                    else
                    {
                        setDownloadLink( m_downloadLink1 );
                    }
                }
            }
        }
    }

    public void handleText( char[] data, int pos )
    {
        String text = new String( data );
        if ( m_downloadLink != null && m_fileName == null )
        {
            //setFileName( text != null ? text.substring(0, text.lastIndexOf(".") ) : null );
			setFileName( m_downloadLink.substring( m_downloadLink.lastIndexOf("/")+1,  m_downloadLink.lastIndexOf(".") ) );
        }
   }

    public String getDownloadLink() {
        return m_downloadLink;
    }

    public void setDownloadLink(String m_downloadLink) {
        this.m_downloadLink = m_downloadLink;
    }

    public String getDownloadLink1() {
        return m_downloadLink1;
    }

    public void setDownloadLink1(String m_downloadLink1) {
        this.m_downloadLink1 = m_downloadLink1;
    }

    public String getDownloadLink2() {
        return m_downloadLink2;
    }

    public void setDownloadLink2(String m_downloadLink2) {
        this.m_downloadLink2 = m_downloadLink2;
    }

    public String getFileName() {
        return m_fileName;
    }

    public void setFileName(String m_fileName) {
        this.m_fileName = m_fileName;
    }
}
